package com.zhaoyuxi.redpack.service.impl;

import com.zhaoyuxi.redpack.entity.TbRedpack;
import com.zhaoyuxi.redpack.entity.TbUserRedpack;
import com.zhaoyuxi.redpack.service.ITbRedpackService;
import com.zhaoyuxi.redpack.service.ITbUserRedpackService;
import com.zhaoyuxi.redpack.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RedisServiceImpl implements RedisService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private ITbRedpackService iTbRedpackService;

    @Autowired
    private ITbUserRedpackService iTbUserRedpackService;


    @Override
    public TbRedpack selectRemain() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        TbRedpack tbRedpack = (TbRedpack) valueOperations.get("redpack");
        return tbRedpack;
    }

    @Override
    public void syncRedPack() {
        redisTemplate.delete("redpack");
        redisTemplate.delete("redpackdetail");
        TbRedpack tbRedpack = iTbRedpackService.selectById(1);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("redpack", tbRedpack);
    }

    @Override
    public void addRedPackDetail() {
        //ListOperations listOperations = redisTemplate.opsForList();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        TbRedpack tbRedpack = (TbRedpack) valueOperations.get("redpack");
        //创建添加对象设置属性
        TbUserRedpack userRedpack = new TbUserRedpack();
        userRedpack.setUserId(tbRedpack.getUserId());
        userRedpack.setRedpackId(tbRedpack.getId());
        userRedpack.setGrabDate(new Date());
        userRedpack.setAmount(new BigDecimal(String.valueOf(tbRedpack.getAmount())));
        iTbUserRedpackService.insert(userRedpack);
        iTbRedpackService.updateById(tbRedpack);
        //添加到redis缓存
        /*listOperations.rightPush("redpackdetail", userRedpack);
        List<TbUserRedpack> redpackdetail = listOperations.range("redpackdetail", 0, -1);
        add(redpackdetail);*/
    }

   /* public void add(List<TbUserRedpack> tbUserRedpack) {
        if (tbUserRedpack.size() >= 1000) {
            ValueOperations valueOperations = redisTemplate.opsForValue();
            TbRedpack tbRedpack = (TbRedpack) valueOperations.get("redpack");
            iTbRedpackService.updateById(tbRedpack);
            for (TbUserRedpack userRedpack : tbUserRedpack) {
                iTbUserRedpackService.insert(userRedpack);
            }
            redisTemplate.delete("redpackdetail");
        }

    }*/

    @Override
    public boolean update(Integer version) {
        synchronized (this){
            ValueOperations valueOperations = redisTemplate.opsForValue();
            TbRedpack tbRedpack = (TbRedpack) valueOperations.get("redpack");
            if (version.intValue() == tbRedpack.getVersion().intValue()) {
                tbRedpack.setRemain(tbRedpack.getRemain() - 1);
                tbRedpack.setVersion(tbRedpack.getVersion() + 1);
                valueOperations.set("redpack", tbRedpack);
                log.info("操作成功");
                return true;
            }
            return false;
        }

    }

    @Override
    public boolean update2() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //开启事务
        redisTemplate.setEnableTransactionSupport(true);
        //监视器（get必须在其后）
        redisTemplate.watch("redpack");
        TbRedpack tbRedpack = (TbRedpack) valueOperations.get("redpack");
        tbRedpack.setRemain(tbRedpack.getRemain() - 1);
        //乐观锁的配置
        tbRedpack.setVersion(tbRedpack.getVersion() + 1);
        //事务开始
        redisTemplate.multi();
        valueOperations.set("redpack", tbRedpack);
        //事务结束返回结果
        List exec = redisTemplate.exec();
        if (exec.size() != 0) {
            //操作成功
            log.info("操作成功");
            return true;
        } else {
            //操作失败
            log.info("操作失败");
            return false;
        }
    }

    @Override
    public void delete() {
        redisTemplate.delete("redpack");
        redisTemplate.delete("redpackdetail");
    }

}
